<?php
/**
 * Desc: Service模板
 * User: zdb
 * Date: 2023/6/5
 * Time: 11:22
 */

namespace app\api\service\develop;
use think\facade\Db;

class DatagridColumnService
{

    private $primaryTable;

    public function __construct()
    {
        $this->primaryTable = "yj_datagrid_column";
    }

    public function findRowById($id)
    {
        return Db::table($this->primaryTable)->alias('a')
            ->where('id', '=', $id)
            ->strict(false)
            ->find();
    }

    public function findRowByData($data)
    {
        $condition = $this->assembleFilterCondition($data);
        return Db::table($this->primaryTable)->alias('a')
            ->where($condition)
            ->strict(false)
            ->find();
    }

    public function getListByManyId($arrId)
    {
        $list = Db::table($this->primaryTable)->alias('a')
            ->whereIn('id', $arrId)
            ->select()
            ->toArray();
        return $list;
    }

    public function assembleFilterCondition($data)
    {
        $condition = ['a.status > 0'];

        if (!empty($data['title'])) {
            $condition[] = "title like '%" . $data['title'] . "%'";
        }

        if (!empty($data['id'])) {
            $condition[] = "a.id = " . $data['id'];
        }

        if (!empty($data['datagrid_id'])) {
            $condition[] = "a.datagrid_id = " . $data['datagrid_id'];
        }

        if (!empty($data['is_in_add'])) {
            $condition[] = "a.is_in_add = " . $data['is_in_add'];
        }

        if (!empty($data['is_in_edit'])) {
            $condition[] = "a.is_in_edit = " . $data['is_in_edit'];
        }

        if (!empty($data['is_require'])) {
            $condition[] = "a.is_require = " . $data['is_require'];
        }

        if (!empty($data['is_fix'])) {
            $condition[] = "a.is_fix = " . $data['is_fix'];
        }

        if (!empty($data['datagrid_code'])) {
            $condition[] = "dc.datagrid_code = '" . $data['datagrid_code'] ."'";
        }

        if (!empty($data['element_type'])) {
            if(is_array($data['element_type'])){
                if(isset($data['element_type'][1]) && is_array($data['element_type'][1])){
                    $condition[] = "a.element_type " .$data['element_type'][0]." ('".implode("','", $data['element_type'][1])."' )";
                }
            }else{
                $condition[] = "a.element_type = '" . $data['element_type'] ."'";
            }

        }

        return empty($condition) ? "" : implode(" AND ", $condition);
    }

    public function getListNoPage($data = [])
    {
        $condition = $this->assembleFilterCondition($data);
        $res = Db::table($this->primaryTable)->alias('a')
            ->leftJoin("yj_datagrid_custom dc", "dc.id = a.datagrid_id")
            ->where($condition)
            ->order('a.sort asc, a.id desc')
            ->select()
            ->toArray();
        return $res;
    }

    public function getListWithPage($data, $pageIndex = 1, $pageSize = 30)
    {
        $offset = ($pageIndex - 1) * $pageSize;
        $condition = $this->assembleFilterCondition($data);
        $res = Db::table($this->primaryTable)->alias('a')
            ->leftJoin("yj_dictionary d", "d.id = a.dictionary_id")
            ->where($condition)
            ->field("a.*, d.dictionary_name_cn")
            ->limit($offset, $pageSize)
            ->order('a.sort asc, a.id desc')
            ->select();
        return $res;
    }

    public function getTotalNum($data)
    {
        $condition = $this->assembleFilterCondition($data);
        $num = Db::table($this->primaryTable)->alias('a')
            ->where($condition)
            ->count();
        return $num;
    }

    public function createRow($data)
    {
        $data['status'] = isset($data['status']) ? $data['status']:1;
        $res = Db::table($this->primaryTable)
            ->strict(false)
            ->insert($data, true);
        return $res;
    }

    public function createManyRow($data)
    {
        $res = Db::table($this->primaryTable)
            ->strict(false)
            ->duplicate(array('datagrid_id', 'column_name'))
            ->insertAll($data, true);
        return $res;
    }

    public function updateRowById($id, $data)
    {
        $res = Db::table($this->primaryTable)->alias('a')
            ->where('id', '=', $id)
            ->strict(false)
            ->save($data);
        return false !== $res ? true : false;
    }

    public function updateListByData($condition, $data)
    {
        $condition = $this->assembleFilterCondition($condition);
        $res = Db::table($this->primaryTable)->alias('a')
            ->where($condition)
            ->save($data);
        return false !== $res ? true : false;
    }

    public function updateListByManyId($arrId, $data)
    {
        $res = Db::table($this->primaryTable)->alias('a')
            ->whereIn('a.id', $arrId)
            ->strict(false)
            ->save($data);
        return false !== $res ? true : false;
    }

    public function removeRowById($id)
    {
        $data = ['status' => 0];
        $res = Db::table($this->primaryTable)
            ->where('id', '=', $id)
            ->strict(false)
            ->save($data);
        return false !== $res ? true : false;
    }

    public function removeListByManyId($arrId)
    {
        $data = ['status' => 0];
        $res = Db::table($this->primaryTable)->alias('a')
            ->whereIn('a.id', $arrId)
            ->strict(false)
            ->save($data);
        return false !== $res ? true : false;
    }

    public function removeListByData($data)
    {
        $condition = $this->assembleFilterCondition($data);
        $data = ['status' => 0];
        $res = Db::table($this->primaryTable)->alias('a')
            ->where($condition)
            ->save($data);
        return false !== $res ? true : false;
    }
}